package com.woniuxy.joylifeforum.controller;

import com.woniuxy.common.entity.ForumForum;
import com.woniuxy.common.utils.JWTUtils;
import com.woniuxy.joylifeforum.service.CommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 社区页面相关，我的关注，活跃社区，搜索
 */
@RestController
public class CommunityController {
    @Autowired
    private CommunityService communityService;

    /*
    获取用户关注社区，活跃社区
     */
    @RequestMapping("/communityConcern")
    public Object communityConcern(HttpServletRequest request) {
        //获取用户id
        //String userId="111";
        String userId = JWTUtils.getUserId(request.getHeader("token"));
        Map<String, Object> result = new HashMap<>();

        //获取用户关注的社区
        List<Map<String, Object>> myConcern = communityService.findMyConcern(userId);
        result.put("code", 200);
        result.put("mes", "OK");
        result.put("data", myConcern);
        //获取活跃社区
        List<Map<String, Object>> activeConcern = communityService.findActiveConcern(userId);
        result.put("active", activeConcern);
        return result;
    }

    /*
    用户添加，取消关注
     */
    @RequestMapping("/concerns")
    public Integer concerns(HttpServletRequest request,String forumId, Integer status) {
        //获取用户id
        //String userId="111";
        String userId = JWTUtils.getUserId(request.getHeader("token"));
        try {
            if (status == 1) {
                //关注社区
                communityService.addConcern(userId, forumId);
                return 1;
            } else {
                //取消关注
                communityService.cancelConcern(userId, forumId);
                return 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
            //异常处理
            return 2;
        }
    }

    /*
    用户搜索社区
     */
    @RequestMapping("/findForum")
    public Object findForum(HttpServletRequest request,String find) {
        //获取用户id
        //String userId="111";
        String userId = JWTUtils.getUserId(request.getHeader("token"));
        List<Map<String, Object>> data = communityService.findForum(find, userId);
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("mes", "OK");
        result.put("data", data);
        return result;
    }
    /*
    查询社区详情
     */
    @RequestMapping("/forumDetails")
    public Object forumDetails(HttpServletRequest request,String forumId) {
        //获取用户id
        //String userId="111";
        String userId = JWTUtils.getUserId(request.getHeader("token"));
        Map<String, Object> data = communityService.findForumDetails(userId, forumId);
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("mes", "OK");
        result.put("data", data);
        return result;
    }
    /*
   管理员申请
    */
    @RequestMapping("/addAdmin")
    public Object addAdmin(HttpServletRequest request,String forumId,String content) {
        //获取用户id
        //String userId="111";
        String userId = JWTUtils.getUserId(request.getHeader("token"));
        Integer status = communityService.addAdminService(userId, forumId, content);
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("mes", "OK");
        result.put("status", status);
        return result;
    }
 /*
   修改社区信息
    */
    @RequestMapping("/submitForum")
    public Object submitForum(HttpServletRequest request,ForumForum forumForum) {
        //获取用户id
        //String userId="111";
        String userId = JWTUtils.getUserId(request.getHeader("token"));
        Integer status = communityService.submitForumService(forumForum);
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("mes", "OK");
        result.put("status", status);
        return status;
    }
    /*
   查询所有社区
    */
    @RequestMapping("/allForum")
    public Object allForum() {
        List<ForumForum> data = communityService.allForumService();
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("mes", "OK");
        result.put("data", data);
        return result;
    }
}
